package com.mdlaser.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.List;

public class JsonUtil {
    /**
     * 把对象序列化成json字符串
     */
    public static String serialObject(Object object){
        ObjectMapper objectMapper = getObjectMapper();
        objectMapper.setSerializationInclusion(Include.NON_NULL);//不要包含空属性，省点流量
        String content;
        try {
            content = objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return content;
    }

    public static String serialObjectWithFormatDate(Object object){
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(format);
        objectMapper.setSerializationInclusion(Include.NON_NULL);//不要包含空属性，省点流量
        String content = "";
        try {
            content = objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            return content;
        }
        return content;
    }

    private static ObjectMapper getObjectMapper() {
        return new ObjectMapper();
    }

    /**
     * json字符串转对象
     */
    public static<T> T deserializeObject(String jsonStr, Class<T> vClass){
        ObjectMapper objectMapper = getObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            return objectMapper.readValue(jsonStr, vClass);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings({"rawtypes" })
    public static Object deserializeObject(byte[] jsonBytes, Class valueType){
        try {
            String jsonStr = IOUtils.toString(jsonBytes, "UTF-8");
            return deserializeObject(jsonStr, valueType);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings({"rawtypes" })
    public static Object deserializeObject(InputStream jsonInputStream, Class valueType){
        try {
            String jsonStr = IOUtils.toString(jsonInputStream, StandardCharsets.UTF_8);
            return deserializeObject(jsonStr, valueType);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> deserializeObjectList(String jsonStr, Class<T> tClass) {
        ObjectMapper objectMapper = getObjectMapper();
        TypeFactory typeFactory = objectMapper.getTypeFactory();
        try {
            return objectMapper.readValue(jsonStr, typeFactory.constructCollectionType(List.class, tClass));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
